object Calendar {
    def main() : Unit = {
        println(new Year().printYear(2009));
    }
}

class Year {
    def startOfYear(year : Int) : Int = {
        // Leap days since beginning of
        // the century
        var cyear : Int;
        var days : Int;
        cyear = year - (100*(year/100));
        days = cyear + (cyear/4);
        days = days - (7*(days/7));
        return days;
    }

    def startOfCentury(year : Int) : Int = {
        // Zeroth day of the century (0 = Sunday)
        var century : Int;
        var mod : Int;
        century = year/100;
        mod = century-(4*(century/4));
        return (2*(3-mod));
    }

    def printMonth(month : Int, year : Int) : String = {
        //Hi !
        var day : Int;
        var start : Int;
        var dow : Int;
        var cmonth : Int;
        var mstart : Int;
        var line : String;
        line = "";
        day = 1;
        cmonth = 1;
        dow = 0;
        start = new Year().startOfYear(year) + new Year().startOfCentury(year);
        if (!(new Year().isLeap(year))) {
            start = start + 1;
        }
        while (cmonth < month) {
            mstart = new Year().daysOfMonth(cmonth, year);
            mstart = mstart - (7*(mstart/7));
            start = start + mstart;
            cmonth = cmonth + 1;
        }
        start = start - (7*(start/7));
        println(" Su Mo Tu We Th Fr Sa");
        while (dow < start) {
            line = line + " " + "  ";
            dow = dow + 1;
        }
        while (day < new Year().daysOfMonth(month,year)+1) {
            if (day < 10) {
                line = line + " ";
            }
            line = line + " " + day;
            day = day + 1;
            dow = dow + 1;
            if (7*(dow/7) == dow) {
                println(line);
                line = "";
            }
        }
        println(line);
        return "";
    }

    def printYear(year : Int) : String = {
        var month : Int;
        var useless : String;
        month = 1;
        while (month < 13) {
            println("");
            println("Month " + month + " - Year " + year);
            useless = new Year().printMonth(month, year);
            month = month + 1;
        }
        return "";
    }

    def daysOfMonth(month : Int, year : Int) : Int = {
        var retour : Int;
        if (month == 2) {
            if (new Year().isLeap(year)) {
                retour = 29;
            } else {
                retour = 28;
            }
        } else if (month == 1 || month == 3 || month == 5 ||
            month == 7 || month == 8 || month == 10 || month == 12){
            retour = 31;
        } else {
            retour = 30;
        }
        return retour;
    }

    def isLeap(year : Int) : Bool = {
        var leap : Bool;
        leap = false;
        if (100*(year/100) == year) {
            if (400*(year/400) == year) {
                leap = true;
            }
        } else {
            if (4*(year/4) == year) {
                leap = true;
            }
        }
        return leap;
    }
}
